package array.simple;

/**
 * 两数之和 II - 输入有序数组
 * 示例 1：
 *
 * 输入：numbers = [2,7,11,15], target = 9
 * 输出：[1,2]
 * 解释：2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。
 * 示例 2：
 *
 * 输入：numbers = [2,3,4], target = 6
 * 输出：[1,3]
 * 示例 3：
 *
 * 输入：numbers = [-1,0], target = -1
 * 输出：[1,2]
 *
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted
 */
public class TwoSumIIInputArrayIsSorted_167 {

    public int[] twoSum(int[] numbers, int target) {
        for (int i = 0; i < numbers.length; i++) {
            int num = target - numbers[i];
            for (int j = i + 1; j < numbers.length; j++) {
                if (num == numbers[j])
                    return new int[]{i + 1, j + 1};
            }
        }
        return null;
    }

    public int[] twoSumOpt(int[] numbers, int target) {
        int l = 0, r = numbers.length - 1;
        while(l < r) {
            int sum = numbers[l] + numbers[r];
            if (sum == target)
                return new int[]{l + 1, r + 1};
            else if (sum > target)
                r--;
            else
                l++;
        }
        return null;
    }


    public static void main(String[] args) {
        TwoSumIIInputArrayIsSorted_167 tw = new TwoSumIIInputArrayIsSorted_167();
        int[] ints = tw.twoSum(new int[]{-1, 0}, -1);
        System.out.println(ints[0] + " " + ints[1]);
    }
}
